package com.gwcloud.leetcode.interview100;

import java.util.Arrays;

/** 
* @ClassName   : Leetcode0324 
* @author      : ganwei 
* @date        : 2020年5月30日 
* @Description : TODO 
*/
public class Leetcode0324 {
    /**
     * -解题思路
     */
	public void wiggleSort(int[] nums) {
		Arrays.sort(nums);
		
        int len=nums.length,i = 0;
        int[] smaller=new int[len%2==0?len/2:(len/2+1)],bigger=new int[len/2];
        //复制
        System.arraycopy(nums,0,smaller,0,smaller.length);
        System.arraycopy(nums,smaller.length,bigger,0,len/2);
        //穿插
        for (; i < len / 2; i++) {
            nums[2*i]=smaller[smaller.length-1-i];
            nums[2*i+1]=bigger[len/2-1-i];
        }
        if (len%2!=0) nums[2*i]=smaller[smaller.length-1-i];

		
    }
    /**
     * -主函数
     */
    public static void main(String[] args) {
    	Leetcode0324 l0324 = new Leetcode0324();
    	int[] nums = {1, 5, 1, 1, 6, 4};
    	l0324.wiggleSort(nums);
    }
}
